#!/bin/bash
#author=WangGuihui
#emial=180482@qq.com

DocKer_VerSion="docker-28.0.4"
SyStem_VerSion=$(cat /etc/os-release | awk -F "=" '/^NAME/ {print $2}' |awk -F '"'  '{print $2}' |awk '{print $1}')
DecomPresSion_Path="/usr/local/src"
echo $SyStem_VerSion
groupadd  docker
mkdir -p /etc/docker

Ubuntu_DocKer () {
	echo "0 * * * *   /usr/sbin/ntpdate  cn.pool.ntp.org  >/dev/null 2>&1"   >> /var/spool/cron/crontabs/root
	systemctl restart cron.service

	modprobe br_netfilter
	cat > /etc/sysctl.d/docker.conf  <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
        sysctl -p /etc/sysctl.d/docker.conf

	cp -f  docker.service   /usr/lib/systemd/system/docker.service
	cp -f  docker.socket /usr/lib/systemd/system/docker.socket
	cp -f  containerd.service  /usr/lib/systemd/system/containerd.service
	cp -f daemon.json /etc/docker/daemon.json 

	tar xf ${DocKer_VerSion}.tgz -C $DecomPresSion_Path
	cd  $DecomPresSion_Path
	for i in $(find $DecomPresSion_Path/docker -maxdepth 1 -type f);do   ln -sf ${i} /usr/local/sbin/ ; done;
        systemctl daemon-reload  
	systemctl enable --now docker.service docker.socket containerd.service 

}


CentOS_DocKer () {
	echo  $DocKer_VerSion 
}

main() {
    # 检查系统版本
    if [ "${SyStem_VerSion}" = "Ubuntu" ]; then
        Ubuntu_DocKer >/dev/null 2>&1
    elif [ "${SyStem_VerSion}" = "CentOS" ]; then
        CentOS_DocKer >/dev/null 2>&1
    else
        echo "此脚本仅限于 Ubuntu 和 CentOS 系统"
        exit 1
    fi

    # 检查 Docker 是否安装
    if systemctl start  docker.service  &> /dev/null; then
        echo "Docker is  installed."
	docker version
    else
        echo "Docker is not installed."
        exit 1
    fi
}

main  

